package com.example.word.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;


import com.example.dict.R;
import com.example.word.bean.Browse;
import com.example.word.bean.Collect;
import com.example.word.bean.Dict;
import com.example.word.bean.User;
import com.example.word.ui.activity.UserDetailActivity;
import com.example.word.util.HttpUtils;
import com.example.word.util.MessageUtils;
import com.example.word.util.SPUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理适配器
 */

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
    private List<User> list = new ArrayList<>();
    private Context mActivity;
    private LinearLayout llEmpty;
    private RecyclerView rvUserList;

    private ItemListener mItemListener;

    public void setItemListener(ItemListener itemListener) {
        this.mItemListener = itemListener;
    }

    public UserAdapter(LinearLayout llEmpty, RecyclerView rvUserList) {
        this.llEmpty = llEmpty;
        this.rvUserList = rvUserList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mActivity = viewGroup.getContext();
        View view = LayoutInflater.from(mActivity).inflate(R.layout.item_rv_user_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        User user = list.get(i);
        if (user != null) {
            String account = (String) SPUtils.get(mActivity, SPUtils.ACCOUNT, "");

            if (user.isOnline()) {
                viewHolder.online.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_yes));
                viewHolder.logout.setVisibility(View.VISIBLE);
            } else {
                viewHolder.online.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_border_radius_fill_gray));
                viewHolder.logout.setVisibility(View.GONE);
            }

            viewHolder.nickName.setText(String.format("%s(%s)", user.getAccount(), user.getNickName()));

            viewHolder.logout.setOnClickListener(v -> {
                AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
                dialog.setIcon(R.drawable.a1);//设置图标
                dialog.setTitle("球球单词");//设置对话框的标题
                dialog.setMessage("确认要将该用户强制下线吗?");
                dialog.setPositiveButton("确定", (dialog12, which) -> {
                    Map<String, String> requestParam = new HashMap<>();
                    requestParam.put("userAccount", user.getAccount());
                    // 调用 API 客户端的方法
                    HttpUtils.sendGetRequest("/userLogout", requestParam, mActivity, Dict.class, response -> {
                        MessageUtils.show("操作成功", mActivity);
                        if (mItemListener != null) {
                            mItemListener.loadItemData();
                        }
                        notifyDataSetChanged();
                    });
                });
                dialog.setNeutralButton("取消", (dialog1, which) -> dialog1.dismiss());
                dialog.show();
            });

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mActivity, UserDetailActivity.class);
                    intent.putExtra("user", user);
                    mActivity.startActivity(intent);
                }
            });
            viewHolder.itemView.setOnLongClickListener(v -> {
                AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
                dialog.setIcon(R.drawable.a1);//设置图标
                dialog.setTitle("球球单词");//设置对话框的标题
                dialog.setMessage("确认要删除该用户吗? 将清除用户所有信息");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Map<String, String> requestParam = new HashMap<>();
                        requestParam.put("account", user.getAccount());
                        // 调用 API 客户端的方法
                        HttpUtils.sendGetRequest("/deleteUser", requestParam, mActivity, Dict.class, response -> {
                            list.remove(user);
                            notifyDataSetChanged();
                            MessageUtils.show("删除成功", mActivity);
                            if (list != null && list.size() > 0) {
                                rvUserList.setVisibility(View.VISIBLE);
                                llEmpty.setVisibility(View.GONE);
                            } else {
                                rvUserList.setVisibility(View.GONE);
                                llEmpty.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                });
                dialog.setNeutralButton("取消", (dialog1, which) -> dialog1.dismiss());
                dialog.show();
                return false;
            });
        }
    }

    public void addItem(List<User> listAdd) {
        //如果是加载第一页，需要先清空数据列表
        this.list.clear();
        if (listAdd != null) {
            //添加数据
            this.list.addAll(listAdd);
        }
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView nickName;
        private ImageView online;
        private Button logout;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            nickName = itemView.findViewById(R.id.nickName);
            online = itemView.findViewById(R.id.online);
            logout = itemView.findViewById(R.id.logout);
        }
    }

    public interface ItemListener {
        void loadItemData();
    }
}
